<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Db;

/**
 * 导入商品管理
 *
 * @icon fa fa-circle-o
 */
class Feditionorderproduct extends Backend
{
    
    /**
     * Feditionorderproduct模型对象
     * @var \app\admin\model\Feditionorderproduct
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Feditionorderproduct;
        $this->view->assign("statusList", $this->model->getStatusList());
    }

    public function import()
    {
        parent::import();
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            // $where2['accounturl']  = array('egt',56);
            $where2['accounturl']  = 56;
            $limit = 1000;
            $list = $this->model
                ->where($where)
                ->where($where2)
                ->order($sort, $order)
                ->paginate($limit);
            $lists = $list->items();
            $newlist = array();

            $Statistics = array();
            // $Statistics['zorder'] = 0;
            $Statistics['new_material'] = 0;
            $Statistics['zproduct'] = 0;
            // $Statistics['zprice'] = 0;
            foreach ($lists as $k=>$v) {
                //成本
                $ordersku = Db::name('fedition_ordersku')->where('product_id',$v['product_id'])->find();
                if(!empty($ordersku)){
                    $v['new_material'] = $ordersku['material'];
                }else{
                    $v['new_material'] = 0;
                }
                //显示五月
                $where_shopify['b.financial_status'] = array('in','paid,refunded,partially_refunded');
                $where_shopify['b.fulfillment_status'] = 'fulfilled';
                $where_shopify['b.created_at'] =  ['between',['1651334400','1654012800']];
                $account = Db::name('fedition_orderquantity')
                    ->field("a.*")
                    ->alias('a')
                    ->join('fa_fedition_ordershopify b', 'b.id = a.order_id', 'left')
                    ->where(
                        [
                            'a.product_id' => $v['product_id'],
                        ]
                    )
                    ->where($where_shopify)
                    ->group('a.order_id')
                    ->select();
                if (!empty($account)) {
                    // $zorder = 0;
                    // $total_price = 0;
                    $quantity = 0;
                    $order_ids = '';
                    foreach ($account as $ka=>$va) {
                        // $zorder += $va['countnum'];
                        // $total_price += $va['total_price'];
                        $order_ids = $order_ids.','.$va['order_id'];
                        $quantity += $va['quantity'];
                        $Statistics['new_material'] += $v['new_material']*$va['quantity'];
                    }
                    
                    $v['order_id'] = $account[0]['order_id'];
                    // $v['zorder'] = $zorder;
                    $v['zproduct'] = $quantity;
                    // $v['zprice'] = $total_price;
                    $newlist[] = $v;

                    // $Statistics['zorder'] += $v['zorder'];
                    $Statistics['zproduct'] += $v['zproduct'];
                    // $Statistics['zprice'] += $v['zprice'];
                }
            }
            $result = array("total" => $list->total(), "rows" => $newlist, "Statistics" => $Statistics);

            return json($result);
        }
        return $this->view->fetch();
    }

}
